//
//  JSONFeedItem+LinkableItem.swift
//  RssFly
//
//  Created by shen on 2023/10/17.
//

import Foundation
import FeedKit

extension JSONFeedItem: LinkableItem {
    /**
     Returns a webpage URL for the item; <link> value is preferred, with fallback to <guid>.
     For example, items from https://devblogs.microsoft.com/feed/landingpage/ do not include a link.
     */
    var linkURL: URL? {
        if let itemLinkString = url?.trimmingCharacters(in: .whitespacesAndNewlines),
           let linkURL = URL(string: itemLinkString)
        {
            return linkURL
        } else if
            let itemGUIDString = id?.trimmingCharacters(in: .whitespacesAndNewlines),
            let linkURL = URL(string: itemGUIDString)
        {
            return linkURL
        }

        return nil
    }
}
